--[[
 -- *名称:SuperBombTypeAnalyzer
 -- *描述:超级炸弹分析器
 -- *版权:Copyright © 2016-2018 深圳市三只小熊科技有限公司 版权所有
 -- *作者:Administrator
 -- *创建日期:2017-11-09
 -- *修改日期:2017-11-09
 ]]
local BobmSuitAnalyzer = require(basePath.."suit/bomb/bombtypeanalyzer");
local SuperBombTypeAnalyzer = pokercore.class(BobmSuitAnalyzer,false);

--关键数量
SuperBombTypeAnalyzer.LIMIT_SIZE = 4 ;
local LIMIT_SIZE = 4 ;
---
 -- [ctor 默认类的构造方法]
 -- @DateTime 2017-09-20
 -- @param     [description]
 -- @return    [description]
 --
function SuperBombTypeAnalyzer:ctor(...)
   SuperBombTypeAnalyzer.super.ctor(self,...) ;
end

---
 -- [dtor 默认类的销毁方法]
 -- @DateTime 2017-09-20
 -- @param     [description]
 -- @return    [description]
 --
function SuperBombTypeAnalyzer:dtor(...)
   SuperBombTypeAnalyzer.super.dtor(self,...) ;  
end


--@Override
function SuperBombTypeAnalyzer:getSuitType()
   return DdzCardTypes.SUPER_BOMB ;
end


--@Override
function SuperBombTypeAnalyzer:doCheckRule(cards,term,result)
	if result:getCardNum() >= SuperBombTypeAnalyzer.LIMIT_SIZE then
		if result:isAllCardLazi() then
			return true ;
		else
			local allValues = result:getNoneLaziValueCounter():getAllValues();
			--只有一种点数
			return (#allValues == 1) and (not result:getNoneLaziValueCounter():hasJoker())
		end
	else
		return false ;
	end
end

--填充癞子，仅当牌中含有癞子，且不全是癞子的时候会进入这里
--@Override
function SuperBombTypeAnalyzer:doAnalysizeLaizi(cards,term,onlyMaxValue)
	local cardsResult = {} ;
	local result = cards:getResult();
	local value = result:getNoneLaziValueCounter():getAllValues()[1]; --获取的键的集合
	--print("#######cards ->",cards);
	-- dump(cards,"cards->");
	for _,card in ipairs(cards:getCardList()) do
		card:changeCard(value);
	end
	local cardsCopy = cards:clone();
	-- dump(cardsCopy,"cardsCopy->");
	-- error("66666666666666666");
	self:analysize(cardsCopy,term);
	table.insert(cardsResult,cardsCopy) ;
	return cardsResult ;
end

--提牌算法

--@Override
function SuperBombTypeAnalyzer:containsPossibleCards(handCards,term) 
	-- print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
	--dump(handCards, "SuperBombTypeAnalyzer handCards->");
	local result = handCards:getResult();
	if (result:getCardNum() >= LIMIT_SIZE) then
		local noneLaiziValues = result:getNoneLaziValueCounter():getAllValues();
		local maxValueNum= 0;
		-- print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
		for _,valueTemp in ipairs(noneLaiziValues) do
			local valueTempNum = result:getNoneLaziValueCounter():getValueCount(valueTemp);
			if (valueTempNum > maxValueNum) then
				maxValueNum = valueTempNum;
			end
		end
		-- print(result:getLaziCardNum());
		-- print(result:getLaziCardNum() > 0);
		-- print(result:getLaziCardNum() + maxValueNum >= LIMIT_SIZE);
		-- print(result:getLaziCardNum() > 0 and result:getLaziCardNum() + maxValueNum >= LIMIT_SIZE);
		--// 炸弹数量足够
		return result:getLaziCardNum() >= 0 and result:getLaziCardNum() + maxValueNum >= LIMIT_SIZE;
	else 
		return false;
	end
end
	
--/** 最多使用癞子的数量 */
local MAX_USE_LAIZI_CARD_NUM = 2;
	
--@Override
function SuperBombTypeAnalyzer:doListPossibleCards(handCards,term) 
	--dump(handCards, "SuperBombTypeAnalyzer handCards->");
	local cardsResult = {};
	--// 非癞子部分
	local result = handCards:getResult();
	local noneLaiziCardList = handCards:subList(1, result:getNoneCardNum());
	local noneLaiziCards = DdzTypeUtil.createCardsCopy(noneLaiziCardList);
	noneLaiziCards:setResult(handCards:getResult():clone());
	--// 癞子部分
 	local laiziCardList = handCards:subList(result:getNoneCardNum()+1, result:getCardNum());
	--dump(laiziCardList,"SuperBombTypeAnalyzer:doListPossibleCards laiziCardList");
	local laiziCards = DdzTypeUtil.createCardsCopy(laiziCardList);
	laiziCards:setResult(handCards:getResult():clone());

	local laiziCardNum = result:getLaziCardNum();
	local maxUseLaiziCardNum = MAX_USE_LAIZI_CARD_NUM < laiziCardNum and MAX_USE_LAIZI_CARD_NUM or laiziCardNum;
	for useLaiziCardNum = 0,maxUseLaiziCardNum do
		local useLaiziCardList = DdzTypeUtil.cloneSublistedCards(laiziCards, 1, useLaiziCardNum); --// 补癞子
		local minNeedCardNum = LIMIT_SIZE - useLaiziCardNum;
		if (minNeedCardNum > 0) then -- // 非纯癞子炸弹
			for needCardNum = minNeedCardNum, 4 do -- // 补2张可以是炸弹，那么3,4张也可以
				local cardsList = {} ;
				if (needCardNum == 1) then
					table.addAll(cardsList,DdzTypeUtil.selectSingleCards(noneLaiziCards, false));
				else 
					table.addAll(cardsList,DdzTypeUtil.selectTupleCountCards(noneLaiziCards, needCardNum));
				end
				for _,usecards in ipairs(cardsList) do
					local card = usecards:get(1);
					for _,laiziCard in ipairs(useLaiziCardList) do
						laiziCard:changeCard(card:getValue());
					end
					local cardsCopy = DdzTypeUtil.cloneCardsAndAddCardList(usecards, useLaiziCardList);
					if (self:match(cardsCopy, term)) then
						self:analysize(cardsCopy, term);
						table.insert(cardsResult,cardsCopy:clone());
					end
				end
			end
		else --// 纯癞子炸弹
			local AllLaiziCardsCopy = DdzTypeUtil.createCardsCopy(useLaiziCardList);
			if (self:match(AllLaiziCardsCopy, term)) then
				self:analysize(AllLaiziCardsCopy, term);
				table.insert(cardsResult,AllLaiziCardsCopy);
			end
			--// 任意牌都能组成炸弹
			for needCardNum = 1,4 do
				local cardsList = {} ;
				if (needCardNum == 1) then
					table.addAll(cardsList,DdzTypeUtil.selectSingleCards(noneLaiziCards, false));
				else 
					table.addAll(cardsList,DdzTypeUtil.selectTupleCountCards(noneLaiziCards, needCardNum));
				end
				for _,usecards in ipairs(cardsList) do
					local card = usecards:get(1);
					for _,laiziCard in ipairs(useLaiziCardList) do
						laiziCard:changeCard(card:getValue());
					end
					local cardsCopy = DdzTypeUtil.cloneCardsAndAddCardList(usecards, useLaiziCardList);
					if (self:match(cardsCopy, term)) then
						self:analysize(cardsCopy, term);
						table.insert(cardsResult,cardsCopy:clone());
					end
				end
			end
		end
	end
	return cardsResult;
end


return SuperBombTypeAnalyzer;

